﻿using System;
using System.Windows.Controls;
using System.Windows.Interactivity;
using System.Windows;

namespace Moria.Silverlight.Controls
{
    public class AlignCanvasBehavior : Behavior<Canvas>
    {
        protected override void OnAttached()
        {
            base.OnAttached();
            this.AssociatedObject.LayoutUpdated += new EventHandler(this.Canvas_LayoutUpdated);
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
            this.AssociatedObject.LayoutUpdated -= this.Canvas_LayoutUpdated;
        }

        void Canvas_LayoutUpdated(object sender, EventArgs e)
        {

            if (this.AssociatedObject != null)
            {
                foreach (FrameworkElement itm in this.AssociatedObject.Children)
                {
                    if (itm == null) continue;
                    if (itm.VerticalAlignment == VerticalAlignment.Bottom) this.AlignBottom(itm);
                    if (itm.VerticalAlignment == VerticalAlignment.Top) this.AlignTop(itm);
                    if (itm.VerticalAlignment == VerticalAlignment.Center) this.AlignMiddle(itm);
                    if (itm.VerticalAlignment == VerticalAlignment.Stretch) this.VStretch(itm);

                    if (itm.HorizontalAlignment == HorizontalAlignment.Left) this.AlignLeft(itm);
                    if (itm.HorizontalAlignment == HorizontalAlignment.Right) this.AlignRight(itm);
                    if (itm.HorizontalAlignment == HorizontalAlignment.Center) this.AlignCenter(itm);
                    if (itm.HorizontalAlignment == HorizontalAlignment.Stretch) this.HStretch(itm);
                }
            }
        }

        private void VStretch(FrameworkElement itm)
        {
            this.AlignTop(itm);
            
            double hgt  = this.Height - (itm.Margin.Top + itm.Margin.Bottom);
            if (hgt >= 0)
                itm.Height = hgt;
        }
        private void HStretch(FrameworkElement itm)
        {
            this.AlignLeft(itm);
            double wdt = this.Width - (itm.Margin.Left + itm.Margin.Right);
            if (wdt >= 0)
                itm.Width = wdt;
        }

        private void AlignMiddle(FrameworkElement itm)
        {
            double vCenter = this.Height / 2;
            double top = itm.ActualHeight / 2;
            top = vCenter-top;
            Canvas.SetTop(itm, top);
        }
        private void AlignBottom(FrameworkElement itm)
        {
            if (itm == null) return;
            double top = itm.ActualHeight;
            top = (this.Height - top);
      //      if (itm.Margin != null) top -=(itm.Margin.Top + itm.Margin.Bottom );
            Canvas.SetTop(itm, top);
        }
        private void AlignTop(FrameworkElement itm)
        {
            double top = 0;
        //    if (itm.Margin != null) top += (itm.Margin.Top + itm.Margin.Bottom);
            Canvas.SetTop(itm, top);
        }
        private void AlignCenter(FrameworkElement itm)
        {
            double hCenter = this.Width / 2;
            double lft = itm.ActualWidth / 2;
            lft = hCenter - lft;
            Canvas.SetLeft(itm, lft);
        }
        private void AlignRight(FrameworkElement itm)
        {
            if (itm == null) return;
            double lft = itm.ActualWidth;
            lft = (this.Width - lft);
            //      if (itm.Margin != null) top -=(itm.Margin.Top + itm.Margin.Bottom );
            Canvas.SetLeft(itm, lft);
        }
        private void AlignLeft(FrameworkElement itm)
        {
            double lft = 0;
            //    if (itm.Margin != null) top += (itm.Margin.Top + itm.Margin.Bottom);
            Canvas.SetLeft(itm, lft);
        }
        private double Height { get { return this.AssociatedObject.ActualHeight; } }
        private double Width { get { return this.AssociatedObject.ActualWidth; } }
    }
}
